import pygame
from pygame.locals import *
import sys
from pygame.sprite import Group
from bar import Bar
from ball import Ball
from arkanoid_reader import Reader
class Game:
    
    TEXTCOLOR = (255, 255, 255)
    font = None
    life = 4
    group = None
    bar = None 
    bar_group = None
    ball = None
    ball_group = None
    screen = None
  

    def __init__(self, stage, screen):
        self.font = pygame.font.SysFont(None, 48)
        self.group = Group()
        self.bar = Bar()
        self.bar_group = Group()
        self.bar_group.add(self.bar)
        self.ball = Ball()
        self.ball_group = Group()
        self.ball_group.add(self.ball)
        reader = Reader()        
        reader.read_cube_file('stage_' + str(stage))
        self.group.add(reader.cubes)
        self.screen = screen
    
    def stage_end(self):
        return len(self.group) == 0
    
    def is_end(self):
        return self.life == 0
    
    def drawText(self, text, x, y):
        textobj = self.font.render(text, 1, self.TEXTCOLOR)
        textrect = textobj.get_rect()
        textrect.topleft = (x, y)
        self.screen.blit(textobj, textrect)
        
    def user_disq(self):
        self.bar.make_long()
        self.life = self.life - 1
        self.ball.handle_mouse_click(False) 
    
    
    def apply_magic(self, magic):
        print(magic)
        
        
        
    
    def has_magic(self, magic):
        print(magic)
    
    
    
    def handle_collisions(self):
        
        for collision in pygame.sprite.groupcollide( self.ball_group, self.group, 0, 0).items():
            collision[0].collide_cube(collision[1][0])
        for cube in pygame.sprite.groupcollide( self.group, self.ball_group, 0, 0).keys():
            cube.hit_by_ball()
            if cube.is_dead():
                self.group.remove(cube)
                if cube.has_magic():
                    self.apply_magic(cube.get_magic())
        for ball in pygame.sprite.groupcollide( self.ball_group, self.bar_group, 0, 0).keys():                
            self.ball.move_up( self.bar.get_velocity() )
    
    def draw_game(self ):
        self.screen.fill((125,0,125))
        self.handle_collisions()
            
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
            if event.type == MOUSEMOTION:
                self.bar.move(event)
                self.ball.move(event)
            if event.type == MOUSEBUTTONDOWN:
                self.ball.handle_mouse_click(True)
        self.bar.calc_velocity()
        self.group.update()
        self.ball.move(None)
        self.group.draw(self.screen)
        self.ball_group.draw(self.screen)
        self.bar_group.draw(self.screen)  
        
        self.drawText('life: %s' % (self.life), 10, 0)
                   
        pygame.display.update()
        pygame.display.flip()
    
    
    
    def end_game(self):
        self.screen.fill((0,0,0))
        self.drawText('press any key', 250, 250)
        
        pygame.display.update()
        pygame.display.flip()  